package com.cam.framework.system.gis.controller;

import com.cam.framework.utils.RestResult;
import com.cam.framework.utils.FileUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/sys/gis")
public class SysGisController {

    /*
    * 数据来源 https://datav.aliyun.com/portal/school/atlas/area_selector
    * */
    public RestResult downloadRoundData() {
        String areaCode = "100000";
        RestTemplate restTemplate = new RestTemplate();
        String requestUri = "https://geo.datav.aliyun.com/areas_v3/bound/{areaCode}{fullFlag}.json";
        Map<String, String> requestParams = new HashMap<String, String>() {{
            put("areaCode", areaCode);
            put("fullFlag", "_full");
        }};
        ResponseEntity<byte[]> responseEntity = restTemplate.getForEntity(requestUri, byte[].class, requestParams);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            try {
                String filePath = FileUtils.getGenJsonDir() + "/" + areaCode + "_full.json";
                File file = new File(filePath);
                FileOutputStream outputStream = new FileOutputStream(file);
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
            } catch (IOException ioException) {
                System.out.println(ioException.getMessage());
            }
        }
        return RestResult.success();
    }
}
